//
//  BSSubTagTableViewController.m
//  baisibudejie
//
//  Created by 陈博文 on 15/12/19.
//  Copyright © 2015年 陈博文. All rights reserved.
//

#import "BSSubTagTableViewController.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import <UIImageView+WebCache.h>
#import "BSSubTagItem.h"
#import "BSSubTageCell.h"
#import <SVProgressHUD.h>

@interface BSSubTagTableViewController ()
/** 订阅数据*/
@property (nonatomic ,strong)  NSMutableArray *subTagArr;

//任务管理者
@property (nonatomic ,weak)  AFHTTPSessionManager *mgr;
@end

@implementation BSSubTagTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置标题
    self.navigationItem.title = @"标签订阅";
    
    [self loadData];
     [self.tableView registerNib:[UINib nibWithNibName:@"BSSubTageCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    // 取消系统分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//设置 tableview 的背景颜色
    self.tableView.backgroundColor = [UIColor redColor];
    
     [SVProgressHUD showWithStatus:@"正在请求..."];
   }


// 界面消失的时候
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    // 隐藏指示器
    [SVProgressHUD dismiss];
    
    // 取消之前的请求
    [_mgr.tasks makeObjectsPerformSelector:@selector(cancel)];
    
    // 销毁会话管理者
    [_mgr invalidateSessionCancelingTasks:NO];
}


-(void)loadData{
    
    // 1.创建会话管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    // 2.创建请求参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"tag_recommend";
    parameters[@"action"] = @"sub";
    parameters[@"c"] = @"topic";
    
    // 3.发送请求Get
    [mgr GET:@"http://api.budejie.com/api/api_open.php" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功就会调用
//        NSLog(@"%@",[responseObject class]);
//        [responseObject writeToFile:@"/Users/chenbowen/Desktop/subTag.plist" atomically:YES];
        
        // 隐藏指示器
        [SVProgressHUD dismiss];
        NSArray *array = responseObject;
        
        //一定要使用模型来解析,要不然报一些,奇怪的错误
        self.subTagArr = [BSSubTagItem mj_objectArrayWithKeyValuesArray:responseObject];
              [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        // 请求失败就会调用
        // 隐藏指示器
        [SVProgressHUD dismiss];

    }];

}


#pragma mark ---------------
#pragma mark - subTagArr
-(NSMutableArray *)subTagArr{
    if (_subTagArr == nil) {
        NSMutableArray *subTagArr = [NSMutableArray array];
        _subTagArr = subTagArr;
        
    }
    return _subTagArr;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return self.subTagArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"cell";
    BSSubTageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    BSSubTagItem *item = self.subTagArr[indexPath.row];
    
    cell.item = item;
    return cell;
}

//设置每行的尺寸

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    BSFunc;
//    NSLog(@"%d",indexPath.row);
    return 80;
}
@end
